import { InputCommand } from "./types";

// InputCompressionManager.ts
export class InputCompressionManager {
    private lastSentInput: InputCommand | null = null;
    private compressionThreshold: number = 0.15; // 输入变化阈值
    
    // 压缩输入数据
    public compressInput(currentInput: InputCommand): InputCommand | null {
        // 第一次发送或重要变化时发送完整数据
        if (!this.lastSentInput || 
            currentInput.isSignificantChange(this.lastSentInput)) {
            
            this.lastSentInput = currentInput;
            return new InputCommand(
                currentInput.sequence,
                currentInput.direction,
                currentInput.position, // 包含位置信息
                currentInput.timestamp

            );
        }
        
        // 微小变化，只发送方向
        const compressedInput = new InputCommand(
            currentInput.sequence,
            currentInput.direction,
            // 不包含位置信息
            null,
            currentInput.timestamp

        );
        
        this.lastSentInput = currentInput;
        return compressedInput;
    }
    
    // 批量压缩输入
    public compressInputBatch(inputs: InputCommand[]): InputCommand[] {
        if (inputs.length === 0) return [];
        
        const compressed: InputCommand[] = [];
        let lastCompressed: InputCommand | null = null;
        
        for (let input of inputs) {
            if (!lastCompressed || input.isSignificantChange(lastCompressed)) {
                const compressedInput = new InputCommand(
                    input.sequence,
                    input.direction,
                    input.position,
                    input.timestamp,

                );
                compressed.push(compressedInput);
                lastCompressed = input;
            } else {
                // 微小变化，使用增量压缩
                const compressedInput = new InputCommand(
                    input.sequence,
                    input.direction,
                    null,
                    input.timestamp
                );
                compressed.push(compressedInput);
            }
        }
        
        return compressed;
    }
}